package com.gangwangchina.android.contactlib.model;

import java.util.HashMap;
import java.util.Map;

import com.gangwangchina.android.contactlib.ContactsInterface;

import android.provider.ContactsContract.CommonDataKinds;


public class Organization extends DataElement {
	public static final String LABEL = CommonDataKinds.Organization.LABEL;
	public static final String TYPE = CommonDataKinds.Organization.TYPE;
	public static final String CONTENT_ITEM_TYPE = CommonDataKinds.Organization.CONTENT_ITEM_TYPE;
	
	public static final String ORGANIZATION = CommonDataKinds.Organization.DATA;
	public static final String COMPANY = CommonDataKinds.Organization.COMPANY;
	public static final String DEPARTMENT = CommonDataKinds.Organization.DEPARTMENT;
	public static final String JOB_DESCRIPTION = CommonDataKinds.Organization.JOB_DESCRIPTION;
	public static final String OFFICE_LOCATION = CommonDataKinds.Organization.OFFICE_LOCATION;
	public static final String TITLE = CommonDataKinds.Organization.TITLE;
	public static final String PHONETIC_NAME = CommonDataKinds.Organization.PHONETIC_NAME;

	

	public String number, label, organization, company, department, job_description,
			office_locatin, title, phonetic_name;
	public int type;

	/**
	 * Creates a new Organization that is not (yet) present in the database
	 * @param type
	 * @param label
	 * @param organization
	 * @param company
	 * @param department
	 * @param job_description
	 * @param office_locatin
	 * @param title
	 * @param phonetic_name
	 * @return
	 */
	public static Organization newOrganization(int type, String label, String organization,
			String company, String department, String job_description,
			String office_locatin, String title, String phonetic_name) {
		return new Organization(type, label, organization, company, department,
				job_description, office_locatin, title, phonetic_name);
	}

	/**
	 * Creates a new Organization that is not (yet) present in the database
	 * @param pcId
	 * @param type
	 * @param label
	 * @param organization
	 * @param company
	 * @param department
	 * @param job_description
	 * @param office_locatin
	 * @param title
	 * @param phonetic_name
	 * @return
	 */
	public static Organization recoverOrganization(long pcId, int type,
			String label, String organization, String company, String department,
			String job_description, String office_locatin, String title,
			String phonetic_name) {
		return new Organization(pcId, type, label, organization,company, department,
				job_description, office_locatin, title, phonetic_name);
	}

	/**
	 * Creates a new Organization that is not (yet) present in the database
	 * @param pcId
	 * @param type
	 * @param label
	 * @param organization
	 * @param company
	 * @param department
	 * @param job_description
	 * @param office_locatin
	 * @param title
	 * @param phonetic_name
	 * @return
	 */
	private Organization(long pcId, int type, String label, String organization, String company,
			String department, String job_description, String office_locatin,
			String title, String phonetic_name) {
		this(pcId);
		this.type = type;
		this.label = label;
		this.company = company;
		this.department = department;
		this.job_description = job_description;
		this.office_locatin = office_locatin;
		this.title = title;
		this.phonetic_name = phonetic_name;
	}

	private Organization(long pcId) {
		super(pcId, CONTENT_ITEM_TYPE);
	}

	private Organization(int type, String label, String organization, String company,
			String department, String job_description, String office_locatin,
			String title, String phonetic_name) {
		this(ContactsInterface.NO_ID, type, label, organization, company, department,
				job_description, office_locatin, title, phonetic_name);
	}

//	@Override
	public String toString() {
		return formattedOrganization();
	}

	public String formattedOrganization() {
		StringBuilder sb = new StringBuilder();
		if (organization != null)
			sb.append(organization).append('\n');
		if (company != null)
			sb.append(company).append('\n');
		if (department != null)
			sb.append(department).append('\n');
		if (job_description != null)
			sb.append(job_description).append('\n');
		if (office_locatin != null)
			sb.append(office_locatin).append('\n');
		if (title != null)
			sb.append(title).append('\n');
		if (phonetic_name != null)
			sb.append(phonetic_name).append('\n');

		return sb.toString();
	}
		
	@Override
	public Map<String, Object> getDataColumns() {				
		Map<String, Object> datas = new HashMap<String, Object>();
		datas.put(ORGANIZATION, formattedOrganization());
		datas.put(COMPANY, company);
		datas.put(DEPARTMENT, department);
		datas.put(JOB_DESCRIPTION, job_description);
		datas.put(OFFICE_LOCATION, office_locatin);
		datas.put(TITLE, title);
		datas.put(PHONETIC_NAME, phonetic_name);
		datas.put(TYPE, type);
		return datas;
	}

	@Override
	public boolean setProperty(String key, Object value) {
		if (key.equals(TYPE))
			type = new Integer((Integer) value);
		else if (key.equals(LABEL))
			label = (String) value;
		else if (key.equals(COMPANY))
			company = (String) value;
		else if (key.equals(DEPARTMENT))
			department = (String) value;
		else if (key.equals(JOB_DESCRIPTION))
			job_description = (String) value;
		else if (key.equals(OFFICE_LOCATION))
			office_locatin = (String) value;
		else if (key.equals(TITLE))
			title = (String) value;
		else if (key.equals(PHONETIC_NAME))
			phonetic_name = (String) value;
		else
			return super.setProperty(key, value);
		return true;
	}

	@Override
	public Object getProperty(String key) {
		if (key.equals(TYPE))
			return String.valueOf(type);
		else if (key.equals(LABEL))
			return label;
		else if (key.equals(COMPANY))
			return company;
		else if (key.equals(DEPARTMENT))
			return department;
		else if (key.equals(JOB_DESCRIPTION))
			return job_description;
		else if (key.equals(OFFICE_LOCATION))
			return office_locatin;
		else if (key.equals(TITLE))
			return title;
		else if (key.equals(PHONETIC_NAME))
			return phonetic_name;
		else
			return super.getProperty(key);
	}
}
